<template>
	<view class="pubBox">
		<scroll-view class="nav-scroll" :enable-flex="true" scroll-with-animation :throttle="false"
			:scroll-left="scrollToLeft" scroll-x swiperWidth="750" @scroll="handleScroll">
			<view class="nav uni-nav">
				<view class="nav-item" :class="swiperIndex == index ? 'nav-item-act' : ''" :key="index"
					v-for="(item, index) in list" @click="taggleNav(index)">
					{{ item.title }}
				</view>
				<view class="nav-line" :style="style"></view>
			</view>
		</scroll-view>
		<view class="swiper">
			<swiper :current="swiperIndex" :duration="300" class="swiper-1" easing-function="linear"
				@change="swiperChange">
				<swiper-item v-for="(item, index) in AllList" :key="index">
					<scroll-view :lower-threshold="80" :refresher-triggered="refreStatus"
						@refresherrefresh="handleRefre" :refresher-enabled="true" class="swiper-scroll" scroll-y="true"
						@scrolltolower="swiperScrollLower">
						<view>
							<view class="houseItem" v-for="(sub_item,index) in item" :key="index">
								<view class="houseImg">
									<image :src="sub_item.houseImg[0]" style="width: 300rpx;height: 200rpx;"></image>
								</view>
								<view class="houseInfo">
									<view class="houseName"><text>{{sub_item.houseName}}</text></view>
									<view class="houseSaleType"><text>{{sub_item.houseType}}</text></view>
									<view class="houseArea"><text>{{sub_item.houseArea}}m²</text></view>
								</view>
								<view class="touPubDetail" @click="toDetail(sub_item)">查看详情</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		created() {
			var that = this
			let user = uni.getStorageSync('user');
			this.user = user
			new Promise((resolve, reject) => {
				uni.request({
					url: "http://www.zphsow.ltd/api/house/getByPublishUserID?publishUserId=" + user.userId,
					methods: 'get',
					success: (res) => {
						resolve(res.data.obj)
					}
				})
			}).then((data) => {
				that.houseList = data
				var houseList = data
				for (var i in houseList) {
					//houseList[i].housePubTime = that.getYMDHMS(houseList[i].housePubTime)
					new Promise((resolve, reject) => {
						houseList[i].houseImg = houseList[i].houseImg.replaceAll(";,", ";").split(";")
						houseList[i].houseImg.pop()
						resolve(houseList[i])
					}).then((data) => {
						switch (data.houseSaleType) {
							case 1:
								that.renthouse.push(data)
								break;
							case 2:
								that.rehandhouse.push(data)
								break;
							case 3:
								that.rehandhouse.push(data)
								break;
							case 4:
								that.newhouse.push(data)
								break;
						}
						
					})

				}
				that.AllList.push(that.renthouse)
				that.AllList.push(that.rehandhouse)
				that.AllList.push(that.newhouse)
				that.AllList.push(that.bussinesshouse)
			})
		},
		data() {
			return {
				AllList: [],
				houseList: [],
				renthouse: [],
				rehandhouse: [],
				newhouse: [],
				bussinesshouse: [],
				user: {},
				swiperIndex: 0, //当前的swiperindex
				navItemWidth: '', //选中下划线的宽度
				navItemLeft: 0, //选中下划线的显示位置
				scrollToLeft: 0, //scrollview需要滚动的距离
				navInfos: [], //所有navitem的节点信息
				parentLeft: 0, //nav盒子的节点信息
				componentWidth: 0, //nav盒子的宽度
				list: [{
						title: '租房',
						content: ['首页-1', '首页-2', '首页-3', '首页-4']
					},
					{
						title: '二手房',
						content: ['测试-1', '测试-2', '测试-3', '测试-4', '测试-5']
					},
					{
						title: '新房',
						content: []
					},
					{
						title: '商用房',
						content: ['hello-1', 'hello-2', 'hello-3', 'hello-4', 'hello-5']
					},
				],
				refreStatus: false
			}
		},
		computed: {
			style() {
				return `width:${this.navItemWidth}px; left:${this.navItemLeft}px`;
			}
		},
		mounted() {
			this.init();
		},
		methods: {
			toDetail(house) {
				house = JSON.stringify(house)
				console.log(house)
				uni.navigateTo({
					url: "/pages/pubDetail/pubDetail?house=" + house
				})
			},
			getYMDHMS(timestamp) {
				let time = new Date(timestamp);
				let year = time.getFullYear();
				let month = time.getMonth() + 1;
				let date = time.getDate();

				if (month < 10) {
					month = "0" + month;
				}
				if (date < 10) {
					date = "0" + date;
				}
				return (
					year +
					"-" +
					month +
					"-" +
					date +
					" "
				);
			},
			// 获取dom信息
			init() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.uni-nav').fields({
					rect: true,
					size: true
				}, res => {
					this.parentLeft = res.left;
					this.componentWidth = res.width + 50;
					console.log('res==>', res);
				});
				query.selectAll('.nav-item').fields({
					rect: true,
					size: true
				}, data => {
					data.forEach((item, index) => {
						if (index == 0) {
							this.navItemWidth = item.width;
							this.navItemLeft = item.left;
						}
						this.navInfos.push({
							width: item.width,
							left: item.left
						});
					});
				});
				query.exec();
			},
			// 点击导航切换swiper
			taggleNav(val) {
				this.swiperIndex = val;
			},
			// 滚动tabs以及移动下划线
			scrollDom() {
				let info = this.navInfos[this.swiperIndex];
				let offsetLeft = info.left - this.parentLeft;
				let scrollLeft = offsetLeft - (this.componentWidth - info.width) / 2;
				this.scrollToLeft = scrollLeft < 0 ? 0 : scrollLeft;
				this.navItemLeft = this.navInfos[this.swiperIndex].left;
				setTimeout(() => {
					this.navItemWidth = info.width;
				}, 50);
			},
			// swiper的index变动
			swiperChange(e) {
				this.swiperIndex = e.detail.current;
				this.scrollDom();
				this.$emit('currentIndex', this.swiperIndex);
			},
			// tabs-scrollview触底
			handleScroll(e) {
				this.scrollDom();
			},
			// swiper-ScrollLower触底
			swiperScrollLower() {
				uni.showToast({
					icon: 'none',
					title: `此时为${this.list[this.swiperIndex].title}触底`
				});
				setTimeout(() => {
					this.getData();
				}, 500);
			},
			// 生成列表数据
			getData() {
				uni.showLoading({
					title: '加载中'
				});
				setTimeout(() => {
					for (let index = 0; index < 10; index++) {
						let arr = this.list[this.swiperIndex].content;
						this.$set(arr, arr.length, Math.random() + '-' + index + this.list[this.swiperIndex]
							.title);
					}
					uni.hideLoading();
				}, 1000);
				console.log(this.list[this.swiperIndex]);
			},
			// 下拉事件
			handleRefre() {
				this.refreStatus = true;
				uni.showLoading({
					title: '下拉加载中'
				});
				setTimeout(() => {
					this.list[this.swiperIndex].content = [];
					for (var i = 0; i < 5; i++) {
						this.list[this.swiperIndex].content.push([this.list[this.swiperIndex].title + '下拉-' + i]);
					}
					uni.hideLoading();
				}, 1000);
				setTimeout(() => {
					this.refreStatus = false;
				}, 1000);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.nav-item {
		display: inline-block;
		width: 150rpx;
		margin: 0 16upx;
		text-align: center;
		transition: color 0.3s ease;
	}

	.nav {
		white-space: nowrap;
		position: relative;
		height: 80upx;
		padding: 20upx 0;
	}

	/* #ifndef APP-NVUE */
	::-webkit-scrollbar {
		display: none;
	}

	/* #endif */

	/* #ifdef H5 */
	// 通过样式穿透，隐藏H5下，scroll-view下的滚动条
	scroll-view ::v-deep ::-webkit-scrollbar {
		display: none;
	}

	/* #endif */

	.nav-item-act {
		color: pink;
		font-weight: bolder;
	}

	.nav-line {
		position: absolute;
		bottom: 0;
		height: 10upx;
		border-radius: 10upx;
		background-color: pink;
		transition: left 0.3s ease;
	}

	swiper {
		height: calc(100vh - 80upx);
	}

	swiper-item {
		text-align: center;
		color: #000;
		position: relative;
		background-color: skyblue;
		color: #fff;
	}

	.swiper-item {
		overflow-y: scroll;
	}

	.swiper-item-list {
		height: 400upx;
		border-bottom: 2upx solid pink;
		line-height: 400upx;
	}

	.swiper-scroll {
		height: 100%;
	}

	page {
		width: 100%;
		height: 100%;
	}

	.houseItem {
		width: 100%;
		height: 350rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		border: 1px solid #FFFFFF;
		margin-bottom: 20rpx;
	}

	.houseImg {
		margin-top: 70rpx;
		margin-left: 20rpx;
	}

	.houseInfo {
		margin-top: 60rpx;
		margin-left: 20rpx;
	}

	.houseName {
		color: red;
		font-size: 40rpx;
		font-weight: 600;
		margin-bottom: 30rpx;
		margin-left: 30rpx;
	}

	.houseSaleType {
		color: white;
		font-size: 35rpx;
	}

	.houseArea {
		font-size: 35rpx;
		margin-left: -60rpx;
		color: pink;
		font-size: 35rpx;
		font-weight: 550;
		margin-top: 30rpx;
	}

	.touPubDetail {
		background-color: white;
		height: 50rpx;
		color: #87ceeb;
		margin-top: 50rpx;
	}

	.touPubDetail:hover {
		height: 60rpx;

	}
</style>
